package main

import (
	"fmt"
	"time"
)

/**
*  User: zhanbei
*  Date: 2025/10/27
*  Description: 翻牌时钟动画 - 显示大字体的当前时间
 */

// 数字的ASCII艺术字体 (7段显示风格)
var digitArt = map[rune][]string{
	'0': {
		" ███ ",
		"█   █",
		"█   █",
		"█   █",
		" ███ ",
	},
	'1': {
		"  █  ",
		" ██  ",
		"  █  ",
		"  █  ",
		" ███ ",
	},
	'2': {
		" ███ ",
		"    █",
		" ███ ",
		"█    ",
		"█████",
	},
	'3': {
		"████ ",
		"    █",
		" ███ ",
		"    █",
		"████ ",
	},
	'4': {
		"█   █",
		"█   █",
		"█████",
		"    █",
		"    █",
	},
	'5': {
		"█████",
		"█    ",
		"████ ",
		"    █",
		"████ ",
	},
	'6': {
		" ███ ",
		"█    ",
		"████ ",
		"█   █",
		" ███ ",
	},
	'7': {
		"█████",
		"    █",
		"   █ ",
		"  █  ",
		"  █  ",
	},
	'8': {
		" ███ ",
		"█   █",
		" ███ ",
		"█   █",
		" ███ ",
	},
	'9': {
		" ███ ",
		"█   █",
		" ████",
		"    █",
		" ███ ",
	},
	':': {
		"     ",
		"  █  ",
		"     ",
		"  █  ",
		"     ",
	},
	' ': {
		"     ",
		"     ",
		"     ",
		"     ",
		"     ",
	},
}

// RunTime 翻牌时钟动画
func RunTime() {
	width, height := getTerminalSize()

	// 隐藏光标
	hideCursor()
	defer showCursor()

	// 清屏
	clearScreen()

	// 显示标题
	moveCursor(width/2-15, 1)
	setColor(94) // 亮蓝色
	fmt.Print("⏰ 翻牌时钟 - 按 Ctrl+C 退出 ⏰")
	resetColor()

	// 设置超时
	done := make(chan bool)
	go func() {
		time.Sleep(60 * time.Second)
		done <- true
	}()

	// 动画循环
	ticker := time.NewTicker(1 * time.Second)
	defer ticker.Stop()

	lastTimeStr := ""

loop:
	for {
		select {
		case <-done:
			break loop
		case <-ticker.C:
			now := time.Now()
			timeStr := now.Format("15:04:05")

			// 只有时间变化时才更新显示
			if timeStr != lastTimeStr {
				lastTimeStr = timeStr

				// 清除时间显示区域
				startY := height/2 - 3
				for i := 0; i < 7; i++ {
					moveCursor(1, startY+i)
					for j := 0; j < width; j++ {
						fmt.Print(" ")
					}
				}

				// 绘制时间
				drawBigTime(timeStr, width, height)

				// 显示日期
				dateStr := now.Format("2006-01-02 Monday")
				moveCursor(width/2-len(dateStr)/2, height/2+5)
				setColor(96) // 亮青色
				fmt.Print(dateStr)
				resetColor()
			}
		}
	}

	// 清屏并显示结束信息
	clearScreen()
	moveCursor(width/2-10, height/2)
	setColor(94)
	fmt.Println("⏰ 时间停止！⏰")
	resetColor()
	moveCursor(1, height)
	time.Sleep(1 * time.Second)
}

// 绘制大字体时间
func drawBigTime(timeStr string, width, height int) {
	// 计算起始位置（居中）
	totalWidth := len(timeStr) * 6 // 每个字符宽5+间隔1
	startX := (width - totalWidth) / 2
	startY := height/2 - 2

	if startX < 1 {
		startX = 1
	}
	if startY < 3 {
		startY = 3
	}

	// 定义翻牌颜色
	colors := []int{91, 92, 93, 94, 95, 96} // 各种颜色
	colorIndex := 0

	// 绘制每个字符
	for charIndex, ch := range timeStr {
		art, exists := digitArt[ch]
		if !exists {
			continue
		}

		// 选择颜色
		if ch == ':' {
			setColor(97) // 冒号用白色
		} else {
			setColor(colors[colorIndex%len(colors)])
			colorIndex++
		}

		// 绘制字符的每一行
		for lineIndex, line := range art {
			moveCursor(startX+charIndex*6, startY+lineIndex)
			fmt.Print(line)
		}
	}

	resetColor()
}
